app.controller('indexController',function ($scope,$controller,indexService) {

    $controller('baseController',{$scope:$scope});


    $scope.isSuperAdmin = false;

    $scope.isYunying = false;

    $scope.isTaxRelated = false;
    $scope.isTax = false;
    $scope.matchValue = '';
    $scope.isMatchValue = false;

    $scope.isChannel = false;

    $scope.isBrandAdmin = false;

    $scope.showLoginName = function () {

      indexService.showLoginName().success(function (response) {

          if (response.code == 200){

              $scope.manager = JSON.parse(JSON.stringify(response.data));

              $scope.matchValue = $scope.manager.matchValue;
              if ($scope.manager.matchValue) {
                  $scope.matchValue = $scope.manager.matchValue;
                  $scope.isMatchValue = true
              }
              response.data.rolesList = null;

              //登录信息存入sessionStorage - base64加密 encodeURIComponent 解决btoa中文加密乱码报错问题
              sessionStorage.setItem("MANAGER",window.btoa(encodeURIComponent(JSON.stringify(response.data))));

              $scope.loginName = response.data.loginName;

              $scope.loginTime = response.data.loginTime;

              //角色列表
              var rolesList = $scope.manager.rolesList;

              if ($scope.isNull(rolesList))
                  rolesList = [];

              for (var i = 0; i < rolesList.length; i++) {
                  var roles = rolesList[i];

                  var roleName = roles.roleName;

                  console.log(roleName === "ROLE_TAX")

                  if (roleName.indexOf("ROLE_SUPERADMIN") != -1) {
                      $scope.isSuperAdmin = true;
                  }else if (roleName.indexOf("ROLE_BRAND_OPERATIONAL") != -1){
                      $scope.isYunying = true;
                  }else if (roleName.indexOf("ROLE_TAX_OPERATIONAL") != -1 ||  roleName.indexOf("ROLE_TAX") != -1){
                      $scope.isTaxRelated = true;
                      if (!(roleName.indexOf("ROLE_TAX_OPERATIONAL") != -1)) {
                          $scope.isTax = true;
                          sessionStorage.setItem("isTax", true)
                      }
                  }else if (roleName.indexOf("ROLE_CHANNEL") != -1) {
                      $scope.isChannel = true;
                  }else if (roleName.indexOf("ROLE_BRAND_ADMIN") != -1) {
                      $scope.isBrandAdmin = true;
                  }

              }
          }
      });
    };

    //修改密码
    $scope.changePassword = function () {

        console.log($scope.isClick);
        if ($scope.isClick){
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        if ($scope.isNull($scope.entity.oldPassword)){

            alert("填写原密码");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if ($scope.isNull($scope.entity.newPassword)){
            alert("填写新密码");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if ($scope.isNull($scope.entity.confirmPassword)){
            alert("填写确认密码");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if (!$scope.entity.newPassword === $scope.entity.confirmPassword) {

            alert("新密码与确认密码不一致");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        $scope.entity.loginName = $scope.loginName;

        indexService.changePassword($scope.entity).success(function (response) {

            if (response.code == 200){

                alert(response.msg);

                location.href = "../login.html";

            } else {
                alert(response.msg);
            }

            $scope.entity = {};

            //还原按钮初始状态
            $scope.isClick = false;
        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });

    };

    //清空SessionStorage
    $scope.clearSessionStorage = function () {
        sessionStorage.clear();
    };
});